<template>
  <div class="dialog">
    <el-dialog :title="title" :visible.sync="visible" :close-on-click-modal="false" width="45%" top="20vh">
      <el-form :model="createForm" label-width="160px" label-suffix=":" :rules="rules" ref="createForm">
        <el-row :gutter="24">
          <el-col :span="22">
            <el-form-item label="角色名称" prop="roleName">
              <el-input v-model.trim="createForm.roleName" placeholder="请输入角色名称"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="22">
            <el-form-item label="角色类型" prop="roleType">
              <el-select v-model="createForm.roleType" placeholder="请选择角色类型" style="width: 100%;">
                <el-option label="系统角色" value="sys" />
                <el-option label="业务角色" value="biz" />
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="22">
            <el-form-item label="角色描述" prop="remark">
              <el-input v-model.trim="createForm.remark" placeholder="请输入角色描述"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="24">
          <el-col :span="22">
            <el-form-item label="排序" prop="orderNo">
              <el-input v-model.trim="createForm.orderNo" placeholder="请输入排序"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeNetWorker()">关 闭</el-button>
        <el-button type="primary" @click="confirmData('createForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  sysRoleAdd,
  sysRoleUpdate
} from '@/api/index'
export default {
  name: "RoleDialog",
  data() {
    return {
      title: "",
      visible: false,
      createForm: {},
      rules: {
        roleName: [
          { required: true, message: "请输入角色名称", trigger: "blur" },
        ],
        roleType: [
          { required: true, message: "请选择角色类型", trigger: "change" },
        ],
        orderNo: [
          { required: true, message: "请输入排序", trigger: "blur" },
        ],
      },
    }
  },
  created() { },
  mounted() { },
  methods: {
    add() {
      this.title = "新增";
      this.createForm = {};
      this.visible = true;
    },
    edit(val) {
      this.title = "编辑";
      this.createForm = Object.assign({}, val)
      this.visible = true;
    },
    confirmData(formData) {
      this.$refs[formData].validate((valid) => {
        if (valid) {
          if (this.createForm.id) {
            sysRoleUpdate(this.createForm).then((res) => {
              const data = res.data
              if (data.code == 0) {
                this.$message.success('修改成功');
                this.$emit('ok');
                this.closeNetWorker();
              } else {
                this.$message.error(data.data || '修改失败')
              }
            })
          } else {
            sysRoleAdd(this.createForm).then((res) => {
              const data = res.data
              if (data.code == 0) {
                this.$message.success('新增成功');
                this.$emit('ok');
                this.closeNetWorker();
              } else {
                this.$message.error(data.data || '新增失败')
              }
            })
          }
        }
      });
    },
    closeNetWorker() {
      this.createForm = {};
      this.visible = false;
    }
  }
};
</script>
<style scoped lang="less"></style>
